import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lahiru
 * Date: 4/7/14
 * Time: 1:32 PM
 * To change this template use File | Settings | File Templates.
 */
public class SaturatedUnits {
    public static void main(String[] args) {
        String hiddenActivationDir = "/media/Bunty/Academic/Research/speaker-effects/hidden-activations/";
//        String listOfSpeakers = "011";
        String listOfSpeakers = "011 014 016 017 018 019 01b 01d 01f 01j 01k 01m 01p 01q 01u 027 028 02c 02e 204 013 " +
                "015 01e 01g 01i 01l 01r 01t 01w 01y 01z 020 021 024 026 029 02b 207 208 20c";

        List<String> speakers = (Arrays.asList(listOfSpeakers.split(" ")));
        int layers = 6;          // TODO this value should be configurable
        for (int i = 1; i <= layers; i++) {

            System.out.println("The hidden layer is " + i);
            double totalActivationCount = 0.0;
            double closerToZero = 0.0;
            double closerToOne = 0.0;
            int utterances = 0;

            for (String spk : speakers) {
                String ha = hiddenActivationDir + "h" + i + "/activations_." + spk + ".ark";
                System.out.println("Analyzing for speaker " + spk);
                try {
                    BufferedReader haBr = new BufferedReader(new FileReader(ha));
                    String line;

                    while ((line = haBr.readLine()) != null) {
                        String hArray[] = line.trim().split(" ");
                        if (hArray.length == 3) {  //Start of an utterance
                            utterances = utterances + 1;
                        } else if (hArray.length == 2048) {
                            for (int j = 0; j < hArray.length; j++) {
                                totalActivationCount = totalActivationCount + 1;
                                if (Double.parseDouble(hArray[j]) < 0.01) {
                                    closerToZero = closerToZero + 1;
                                } else if (Double.parseDouble(hArray[j]) > 0.99) {
                                    closerToOne = closerToOne + 1;
                                }
                            }
                        } else if (hArray.length == 2049) {
                            for (int j = 0; j < hArray.length - 1; j++) {
                                totalActivationCount = totalActivationCount + 1;
                                if (Double.parseDouble(hArray[j]) < 0.01) {
                                    closerToZero = closerToZero + 1;
                                } else if (Double.parseDouble(hArray[j]) > 0.99) {
                                    closerToOne = closerToOne + 1;
                                }

                            }
                        }


                    }
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }

            System.out.println("For the hidden layer " + i);
//            System.out.println("The totalActivationCount " + totalActivationCount);
//            System.out.println("The closerToZero " + closerToZero);
//            System.out.println("The closerToOne " + closerToOne);

            double closerToZeroPercentage = (closerToZero / totalActivationCount) * 100;
            double closerToOnePercentage = (closerToOne / totalActivationCount) * 100;
            System.out.println("The closerToZero Percentage " + closerToZeroPercentage);
            System.out.println("The closerToOne Percentage " + closerToOnePercentage);
            System.out.println("The ha analysis is completed for hidden layer  " + i);


        }


    }
}
